// stores/dialog.ts
import { defineStore } from 'pinia';
import { useAtlasStore } from '@/store/modules/atlas';
import * as Cesium from 'cesium';
import { BlinkingPointManager } from '@/utils/BlinkingPointManager';

const BlinkingPointManagerInstance = new BlinkingPointManager();

export const useDialogStore = defineStore('dialog', {
  state: () => ({
    dialogMap: {
      siteModule: false,
      repairTeamModule: false,
      alertOverviewModule: false,
      monitoringEquipmentModule: false,
      monitoringForceModule: false,
      realTimeMonitoringModule: false,
      thirdPartyVandalismAlarmsModule: false,
      topAlertOverviewModule: false,
      topAlarmTypeModule: false,
      alarmTypeModule: false
      // 可以继续添加其他模块
    },
    currentDialog: null as string | null
  }),
  actions: {
    openDialog(dialogName: string) {
      // 关闭之前打开的对话框（替代循环全部设为false）
      if (this.currentDialog) {
        this.dialogMap[this.currentDialog] = false;
      }

      // 打开新对话框
      this.currentDialog = dialogName;
      this.dialogMap[dialogName] = true;
    },
    closeAll() {
      this.currentDialog = null;
      Object.keys(this.dialogMap).forEach((key) => {
        this.dialogMap[key] = false;
      });
      useAtlasStore().atlasChange('clear');
    },
    flyTo({ longitude, latitude }, height = 2000, entity = null) {
      window.viewer.camera.flyTo({
        destination: Cesium.Cartesian3.fromDegrees(Number(longitude), Number(latitude), height)
      });
      if (entity) {
        BlinkingPointManagerInstance.toggleBlinking(entity, window.viewer);
      }
    }
  }
});
